<template>
  <div class="app-container">
    <!-- 对象搜索 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="请求接口" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入请求接口"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求方法" prop="method">
        <el-input
          v-model="queryParams.method"
          placeholder="请输入请求方法"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <y-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          :options="sys_enable_disable"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 对象操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['sys:whitelist:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sys:whitelist:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sys:whitelist:delete']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <y-export
          :url="'sys/post/export/'"
          :ids="ids"
          :filename="'post_data'"
          v-hasPermi="['sys:whitelist:export']"
        />
      </el-col>
      <y-right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      />
    </el-row>

    <!-- 对象列表 -->
    <el-table
      ref="tableRef"
      v-loading="loading"
      row-key="id"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        reserve-selection
      />
      <el-table-column label="序号" width="100" align="center" type="index">
        <template #default="scope">
          {{ scope.$index + (queryParams.page - 1) * queryParams.limit + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="请求地址"
        min-width="300"
        align="center"
        prop="url"
        v-if="columns[0].visible"
      />
      <el-table-column
        label="请求方法"
        align="center"
        prop="method"
        v-if="columns[1].visible"
      >
        <template #default="scope">
          <y-dict-tag :options="sys_http_method" :value="scope.row.method" />
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        v-if="columns[2].visible"
      >
        <template #default="scope">
          <y-dict-tag :options="sys_enable_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="create_time"
        width="180"
        v-if="columns[3].visible"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" with="100" align="center">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['sys:whitelist:edit']"
            />
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['sys:whitelist:delete']"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <y-pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.limit"
      @pagination="getList"
    />

    <!-- 添加或修改白名单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="whitelistRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="请求接口" prop="url">
          <el-input v-model="form.url" placeholder="请输入请求接口" />
        </el-form-item>
        <el-form-item label="请求方法" prop="method">
          <y-select
            v-model="form.method"
            placeholder="请选择请求方法"
            :options="sys_http_method"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_enable_disable"
              :key="dict.value"
              :value="dict.value"
              >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="5"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Whitelist">
import {
  listWhitelist,
  addWhitelist,
  delWhitelist,
  getWhitelist,
  updateWhitelist,
} from "@/api/sys/whitelist";

const { proxy } = getCurrentInstance();
const { sys_enable_disable, sys_http_method } = proxy.useDict(
  "sys_enable_disable",
  "sys_http_method"
);

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

// 列显隐信息
const columns = ref([
  { key: 0, label: `请求地址`, visible: true },
  { key: 1, label: `请求方法`, visible: true },
  { key: 2, label: `状态`, visible: true },
  { key: 3, label: `创建时间`, visible: true },
]);

const data = reactive({
  form: {},
  queryParams: {
    url: undefined,
    method: undefined,
    status: undefined,
    page: 1,
    limit: 10,
  },
  rules: {
    url: [
      { required: true, message: "白名单接口地址不能为空", trigger: "blur" },
    ],
    method: [
      {
        required: true,
        message: "白名单接口请求方法不能为空",
        trigger: "blur",
      },
    ],
    status: [
      { required: true, message: "白名单状态不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询白名单列表 */
function getList() {
  loading.value = true;
  listWhitelist(queryParams.value).then((response) => {
    postList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    url: undefined,
    method: undefined,
    sort: 0,
    status: "0",
    remark: undefined,
  };
  proxy.resetForm("whitelistRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加白名单";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getWhitelist(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改白名单";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["whitelistRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateWhitelist(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWhitelist(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    if (row.id) {
      await proxy.$modal.confirm(`确认是否删除 "${row.name}" ？`);
    } else {
      await proxy.$modal.confirm("确认是否删除所有已勾选信息？");
    }
    ids.value = row.id ? [row.id] : ids.value;
    await delWhitelist(ids.value);
    proxy.$modal.msgSuccess("删除成功");
    proxy.$refs.tableRef.clearSelection();
    getList();
  } catch (error) {
    console.error(error);
  }
}

onMounted(() => {
  getList();
});
</script>
